/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.tab;



import lombok.Getter;
import lombok.Setter;

import org.apache.log4j.Level;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.wb.swt.SWTResourceManager;

import com.google.code.cubeirc.Main;
import com.google.code.cubeirc.base.BaseTab;
import com.google.code.cubeirc.common.Utils;
import com.google.code.cubeirc.debug.DebuggerQueue;

public final class TabManager {

	@Getter @Setter
	private static CTabFolder tabfolder;


	@SuppressWarnings("rawtypes")
	public static void addTab(String tabname, String image, Boolean closable, String classname, Object[] params, Class[] args)
	{

		if (getTabfolder() != null)
		{
			if (!checkTabExists(tabname))
			{
				

				final BaseTab control = Utils.createBaseTabfromClass(classname, params, args);				
				DebuggerQueue.addDebug(TabManager.class.getName(), Level.DEBUG, "Adding tab %s control type %s", tabname,control.toString());


				CTabItem item = new CTabItem(getTabfolder(), SWT.NORMAL);
				item.setShowClose(closable);

				if (image != null | !image.isEmpty())
					item.setImage(SWTResourceManager.getImage(Main.class,image));

				if (control instanceof BaseTab)
				{
					control.setTabitem(item);
					control.Init();
				}
				item.addDisposeListener(new DisposeListener() {

					@Override
					public void widgetDisposed(DisposeEvent arg0) {

						control.Close();
					}
				});


				item.setText(tabname);
				control.setParent(item.getParent());
				item.setControl(control);			


				getTabfolder().setSelection(item);
			}
			else
			{
				DebuggerQueue.addDebug(TabManager.class.getName(), Level.WARN, "Tab %s exist!", tabname);
				setSelectionTab(tabname);
			}
		}
		else
		{
			DebuggerQueue.addDebug(TabManager.class.getName(), Level.ERROR, "I can't add item on TabFolder becouse is null");
		}

	}

	public static BaseTab getTabfromTitle(String title)
	{
		BaseTab ret = null;

		Control[] tabs = getTabfolder().getTabList();

		for (int i=0;i<tabs.length;i++)
		{
			if (tabs[i] instanceof BaseTab)
			{
				BaseTab tb = (BaseTab)tabs[i];
				if (tb.getTabName().equals(title))
				{
					ret = tb;
				}
			}
		}
		return ret;
	}

	/**
	 * Gets the tab itemfrom title.
	 *
	 * @param name the name
	 * @return the tab itemfrom title
	 */
	public static CTabItem getTabItemfromTitle(String name)
	{
		BaseTab tb = getTabfromTitle(name);
		if (tb != null)
		{
			return tb.getTabitem();
		}
		else
		{
			return null;
		}
	}


	/**
	 * Check tab exists.
	 *
	 * @param name the name
	 * @return the boolean
	 */
	public static Boolean checkTabExists(String name)
	{
		Boolean ret = false;
		if (getTabfromTitle(name) != null)
		{
			ret = true;
		}

		return ret;
	}

	public static void setSelectionTab(String name)
	{
		getTabfolder().setSelection(getTabItemfromTitle(name));
	}


}
